/**
 * Created by evil on 17-2-23.
 */

$(function ()
{
    employee.init();
    employee.isAll();
    employee.addBtn();
});

var employee = {
    init: function ()
    {
        $('#setting-css').attr('href', '/static/css/setting_employee.css');
        employeeList.getData();
        $('#employee-list').removeClass('hidden');
    },
    isAll: function ()
    {
        $('#is-all-btn').click(function ()
        {
            if (employeeList.isAll)
            {
                $(this).removeClass('btn-warning');
                $(this).addClass('btn-info');
                $(this).find('span').removeClass('glyphicon-resize-small');
                $(this).find('span').addClass('glyphicon-resize-full');
                $(this).find('span').html(' 显示全部');
                employeeList.isAll = false;
                employeeList.getData();
            } else
            {
                $(this).removeClass('btn-info');
                $(this).addClass('btn-warning');
                $(this).find('span').removeClass('glyphicon-resize-full');
                $(this).find('span').addClass('glyphicon-resize-small');
                $(this).find('span').html(' 收起全部');
                employeeList.isAll = true;
                employeeList.getData();
            }
        });
    },
    addBtn: function ()
    {
        $('#add-btn').click(function ()
        {
            $('#employee-dialog').modal('show');
            employeeDialog.show();
        });
    }
};


var employeeList = new Vue({
    el: '#employee-list',
    data: {
        isAll: false,
        list: []
    },
    methods: {
        getData: function ()
        {
            $.get('/setting/employee/get/list', {
                isAll: employeeList.isAll
            }, function (list)
            {
                employeeList.list = list;
            })
        },
        fix_employee: function (employee)
        {
            if (employee.leaveDateTime == null)
            {
                $('#employee-dialog').modal('show');
                employeeDialog.show(employee);
            }
            else
                toastr.warning('“' + employee.name + '”' + '已经离职，无法修改！');
        }
    }
});

var employeeDialog = new Vue({
    el: '#employee-dialog',
    data: {
        title: '',
        employee: {},
        typeOfWork: {},
        typeOfWorkList: []
    },
    methods: {
        clean: function ()
        {
            this.employee = {};
            this.typeOfWork = {};
        },
        show: function (employee)
        {
            this.clean();
            if (employee == null)
            {
                this.title = '加载中，请稍后……';
                $.get('/setting/employee/get/typeofwork/list', function (list)
                {
                    employeeDialog.typeOfWorkList = list;
                    employeeDialog.title = '新增员工';
                });
            }
            else
            {
                this.title = '加载中，请稍后……';
                $.get('/setting/employee/get/typeofwork/list', function (list)
                {
                    employeeDialog.typeOfWorkList = list;
                    employeeDialog.employee = employee;
                    employeeDialog.typeOfWork = employee.typeOfWork;
                    employeeDialog.title = '修改员工 ID:' + employee.employeeId;
                });
            }
        },
        leave: function ()
        {
            $.post('/setting/employee/post/leave', {
                employeeId: this.employee.employeeId
            }, function (date)
            {
                if (date == null || date == '')
                    toastr.error('离职失败！');
                else
                {
                    $('#employee-dialog').modal('hide');
                    toastr.success('员工 “' + employeeDialog.employee.name
                        + '” 已经离职<br>时间：' + date);
                    employeeList.getData();
                }
            });
        },
        submit: function ()
        {
            cleanInputHelp();

            var sum = 0;

            sum += isInputNull($('#name'));
            sum += isInputNull($('#typeOfWorkId'));
            sum += isInputNull($('#salary'));

            if (sum != 3)
                return null;

            if (this.employee.employeeId == null)
            {/*新增*/
                $.post('/setting/employee/post/new', {
                    name: this.employee.name,
                    typeOfWorkId: this.typeOfWork.typeOfWorkId,
                    salary: this.employee.salary,
                    bonus: this.employee.bonus
                }, function (id)
                {
                    if (id != null || id != '')
                    {
                        $('#employee-dialog').modal('hide');
                        toastr.success('新加入员工成功！<br>ID:' + id);
                        employeeList.getData();
                    } else
                    {
                        toastr.error('新加入员工失败！');
                    }
                });
            } else
            {/*修改*/
                $.post('/setting/employee/post', {
                    employeeId: this.employee.employeeId,
                    name: this.employee.name,
                    typeOfWorkId: this.typeOfWork.typeOfWorkId,
                    salary: this.employee.salary,
                    bonus: this.employee.bonus
                }, function (data)
                {
                    if (data == true)
                    {
                        $('#employee-dialog').modal('hide');
                        toastr.success('员工信息更新成功！<br>ID:' + employeeDialog.employee.employeeId);
                        employeeList.getData();
                    } else
                    {
                        toastr.error('员工信息更新失败！');
                    }
                });
            }
        }
    }
});

function isInputNull(it, help)
{
    if (it.val() == null || it.val().toString().trim() == '')
    {
        it.parent().parent().addClass('has-error');
        if (help == null)
            it.parent().find('span').text('输入为空！');
        else
            it.parent().find('span').text(help);

        return 0;
    }
    return 1;
}

function cleanInputHelp()
{
    var input = $('.input');
    input.find('.form-group').removeClass('has-error');
    input.find('.help-block').empty();
}
